/**
 * 使用说明:
   js: $(function(){
            setCatalogTreeMenu("catalog");
       })
   html:<div class="form-group">
            <label class="col-sm-2 control-label">目录管理：</label>
            <div class="col-sm-4">
                <input class="form-control" type="text" id="catalogName" name="catalogName" onclick="showCatalogTreeMenu(this.id);" readonly>
                <input type="hidden" id="catalogId" name="catalogId">
            </div>
        </div>
 *
 */
var getList_url = "/lrm/controller/catalog/getCatalogList";
var treeMenuDiv = "catalogTreeMenu_div";
var treeMenu = "catalogTreeMenu";
$(function(){
    var div = '<div id="catalogTreeMenuDiv" class="ztreemenu" style="display:none; position: absolute;z-index: 1888;">';
    div+='<ul id="catalogTreeMenu" class="ztree" style="margin-top:0;height:300px;overflow: auto;"></ul></div>';
    $("#mainScroll").after(div);
})
//加载部门树形下拉菜单
function setCatalogTreeMenu(input){
    var setting = {
        view: {
            dblClickExpand:false
        },
        data:{
            simpleData:{
                enable: true
            }
        },
        callback: {
            onClick: getMethod(input)
        }
    };
    //加载部门树形节点
    $.post(getList_url,function(data){
        var catalogList = data.catalogList;
        var companyName = data.companyName;
        var zNodes = "[{ id:'0', pId:'0', name:companyName, open:true, noRemoveBtn: true, noRenameBtn: true}";
        $.each(catalogList,function(i,catalog){
            var id = catalog.catalogId;
            var pId = catalog.parentCatalogId==''?'0': catalog.parentCatalogId;
            var name = catalog.catalogName;
            var orderNo = catalog.catalogOrderNo;
            zNodes+=",{id: '"+id+"', pId: '"+pId+"', name: '"+name+"', orderNo: "+orderNo+", open: true}";
        });
        zNodes+="]";
        $.fn.zTree.init($("#catalogTreeMenu"), setting, eval(zNodes));
    })
}
function getMethod(input) {
    return function(e, treeId, treeNode){
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        var nodes = zTree.getSelectedNodes();
        var nodeId = nodes[0].id;
        var nodeName = nodes[0].name;
        $("#"+input+"Id").val(nodes[0].id);
        $("#"+input+"Name").val(nodes[0].name);
        hideCatalogMenu();
    };
}
function showCatalogTreeMenu(inputId){
    var input = $("#"+inputId);
    var inputOffset = $(input).offset();
    $("#catalogTreeMenuDiv").css({left:inputOffset.left + "px", top:inputOffset.top + input.outerHeight() + "px",width:input.width()+26+"px"}).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
}
function hideCatalogMenu(){
    $("#catalogTreeMenuDiv").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event){
    if (!(event.target.id == "menuBtn" || event.target.id == catalogTreeMenuDiv || $(event.target).parents("#catalogTreeMenuDiv").length>0)) {
        hideCatalogMenu();
    }
}